package com.cleargroup.magic.cleara.c.command;

import android.content.Context;
import android.text.TextUtils;

import com.cleargroup.magic.cleara.c.BatteryHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ThumbCommand extends ExaminableCommand implements
        ScanManageListener {
    public static final String TAG = ThumbCommand.class.getSimpleName();
    int progressTotal = 5000;
    int progressIndex = 0;
    boolean enable_recycle = false;
    private long totalSize;

    public ThumbCommand(Context context) {
        super(context);
    }

    public void examine() {
        try {
            if (!ScanManage.isSupportMediaStore()) {
                String path = EnvironmentUtil.EXTERNAL_STORAGE;
                ScanManage.getInstance(getContext()).scanThumb(path, this);
                totalSize = ScanManage.getInstance(getContext()).thumbTotalSize;
            } else {
                StoreManager.get(getContext()).queryThumb(this);
            }

            if (getListener() != null) {
                getListener().onExamined(getContext(), this, -1, totalSize);
            }
        } catch (Exception e) {
        }
    }

    public void examine(ArrayList<String> pathList) {
        try {
            if (pathList == null || pathList.size() == 0) {
                return;
            }
            for (int i = 0; i < pathList.size(); i++) {
                if (!ScanManage.isSupportMediaStore()) {
                    ScanManage.getInstance(getContext()).scanPath(pathList.get(i), this);
                } else {
                    StoreManager.get(getContext()).queryThumb(this, pathList.get(i));
                }
            }

        } catch (Exception e) {
        }
    }

    public void onNodeScan(int type, File node) {
        if (isCanceled()) {
            return;
        }

        if (node.isDirectory()) {
            return;
        }

        Progress progress = new Progress(this);
        progress.setMsg(node.getAbsolutePath());
        progress.setArg1(progressIndex++);
        progress.setArg2(progressTotal);

        if (type == TrashHelper.TRASH_THUMBNAIL) {
            String fileName = node.getName();
            String filePath = node.getAbsolutePath();
            long fileSize = node.length();
            totalSize += fileSize;
            progress.setObj(new TrashItem(filePath, fileName, fileSize));
        }

        if (getListener() != null) {
            getListener().onExamining(progress);
        }
    }

    public void setCanceled(boolean isCanceled) {
        super.setCanceled(isCanceled);
        if (isCanceled()) {
            ScanManage.getInstance(getContext()).cancel();
        }
    }

    public void execute(List... lists) {
        if (lists.length > 0) {
            for (int i = 0; i < lists[0].size(); i++) {
                BatteryHelper.delete((String) lists[0].get(i));
            }
        }
    }

    public void execute(String uri) {
        if (!TextUtils.isEmpty(uri)) {
            BatteryHelper.delete(uri);
        }
    }
}
